package com.example.project3;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
	
    private Button sendButton;
    private EditText messageText;
    private String messageToSend;
    private Context context; 
    static final String EXTRA_MESSAGE = "com.example.Project3.MESSAGE";


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		 messageText = (EditText) findViewById(R.id.msgToServer);
	     sendButton = (Button) findViewById(R.id.sendButton);
	     context = this.getApplicationContext();
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	public void sendMessage(View v) {
		messageToSend = messageText.getText().toString();		        
        new ClientSender(context).execute(messageToSend);
	}
	
    private class ClientSender extends AsyncTask<String, Void, Socket> {
		private Socket socket;
        private String answer;
        private Context context;
        private BufferedWriter out;
        private InputStream in;

        public ClientSender(Context context) {
            this.context = context;
            socket = null;
            out = null;
            in = null;
            answer = null;
        }

        @Override
        protected Socket doInBackground(String... params) {
            try {
                if (socket == null) {
                	InetAddress serverAddr = InetAddress.getByName("130.207.114.21");  //or for lovalhost "10.0.2.2"
                    socket = new Socket(serverAddr, 1218);
        	           
    	            out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
    	            out.write(params[0]);
                    out.flush();
                    Log.d(null, "About to receive");
    	            in = socket.getInputStream();
    	            Log.d(null, "Almost there");
    	            

    	            byte[] byteString = new byte[33];
    	            in.read(byteString);   	            
    	            byteString[32] = '\0'; 
    	            answer = new String(byteString);
    	            
    	            Log.d(null, "Got it!");
    	            Log.d(null,"Server: " + answer);
  
    	            
    	            StringBuilder sb = new StringBuilder();
    	            
    	            for(int i = 0; i<32; i++){
    	            	sb.append(String.format("%02x", byteString[i]&0xff));
    	            }
    	             
    	            Log.d(null, "Byte array to Hex String in Java: " + sb); 
    	            
    	            in.close();
    	            
    	            Intent intent = new Intent(context, HashRecv.class);
    	            intent.putExtra(EXTRA_MESSAGE, sb.toString());
                    startActivity(intent); 
                }
            } catch (IOException e) {
            	Log.e("Connection: ", "Failed to connect...");
                e.printStackTrace();
            }

            return socket;
        }

    }

}
